﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Site_CustomAdminPages : BaseSitePage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[]{Acms.Bll.Role.Roles.Developer}; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        dsPages.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.CustomAdminPage>>(dsPages_SelectData);
        btnInsert.Click += new EventHandler(btnInsert_Click);
        dsPages.BeforeUpdating += new EventHandler<Acms.DataControls.BeforeUpdatingEventArgs>(dsPages_BeforeUpdating);
        grid.ItemCommand += new Telerik.Web.UI.GridCommandEventHandler(grid_ItemCommand);
        grid.ItemDeleted += new Telerik.Web.UI.GridDeletedEventHandler(grid_DataChanged);
        grid.ItemUpdated += new Telerik.Web.UI.GridUpdatedEventHandler(grid_DataChanged);
        grid.ItemInserted += new Telerik.Web.UI.GridInsertedEventHandler(grid_DataChanged);
    }

    void grid_DataChanged(object source, Telerik.Web.UI.GridDataChangeEventArgs e)
    {
        if (e.Exception == null)
        {
            //AcmsSite.RefreshCustomAdminPages();
        }
    }


    void grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "MoveUp" || e.CommandName == "MoveDown")
        {
            grid.EditIndexes.Clear();
            int change = 1;
            if (e.CommandName == "MoveUp")
            {
                change = -1;
            }
            List<CustomAdminPage> pages = CustomAdminPage.GetAllBySiteId(AcmsContext.SiteId);

            CustomAdminPage.Move(pages, e.Item.DataSetIndex, e.Item.DataSetIndex + change);

            grid.Rebind();
            grid.SelectedIndexes.Clear();
            grid.SelectedIndexes.Add(e.Item.ItemIndex + change);

        }
    }

    void dsPages_BeforeUpdating(object sender, Acms.DataControls.BeforeUpdatingEventArgs e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            e.Values.Add("SiteId", AcmsContext.SiteId);
        }
    }

    void btnInsert_Click(object sender, EventArgs e)
    {
        grid.EditIndexes.Clear();
        grid.MasterTableView.IsItemInserted = true;
        grid.Rebind();
    }

    void dsPages_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.CustomAdminPage> e)
    {
        e.SetData(AcmsContext.Site.CustomAdminPages);
    }
}
